package net.accenture.maven.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HolaMundoSimpleServlet
 */
public class HolaMundoSimpleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HolaMundoSimpleServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.err.println("doGet()");
		response.setContentType("text/html");
		PrintWriter out;
		try {
			System.out.println("try");
			out = response.getWriter();
			// request.getParameter("contrasenia");
			out.println("<html>");
			out.println("<h1>Bienvenido</h1> ");
			out.println("<h2>Ingreso de datos</h2> ");
			
			out.println("<form method=\"post\">");
			out.println("<input type=\"text\" name=\"nombre\"></input>");
			out.println("</form>");
			
			if (request.getSession().getAttribute("nombreUsuario") != null) {
				String nombre = (String)request.getSession().getAttribute("nombreUsuario");
				out.println("<strong>Usted es <em>" + nombre + "</em></strong>");
			}
			
			out.println("</html>");
		} finally {
			System.err.println("finally");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.err.println("doPost()");
		response.setContentType("text/html");
		PrintWriter out;
		try {
			String nombre = request.getParameter("nombre");
			System.out.println("try");
			out = response.getWriter();
			out.println("Hola " + nombre);
			
			request.getSession().setAttribute("nombreUsuario", nombre);
		} finally {
			System.err.println("finally");
		}
	}

}
